var mongoose = require('mongoose')
var Schema = mongoose.Schema

const noteSchema = Schema({
  user: {
    type: Schema.Types.ObjectId,
    ref: 'User'
  },
  book: {
    type: Schema.Types.ObjectId,
    ref: 'Book'
  },
  noteText: String,
  created_at: Date,
  updated_at: Date
})

noteSchema.pre('save', function (next) {
  const note = this
  if (!note.isModified || !note.isNew) {
    next()
  } else {
    const currentDate = new Date()
    note.updated_at = currentDate
    if (!note.created_at)
      note.created_at = currentDate
    next()
  }
})

module.exports = mongoose.model('Note', noteSchema)
